// Element Plus相关工具类

import {ElLoading, ElMessageBox, ElNotification} from "element-plus";

export class ElementUtils {
    // 启动一个loading动画
    static loading(options) {
        return ElLoading.service({
            text: "请求中...",
            background: 'rgba(0, 0, 0, 0.8)',
            ...options,
        });
    }

    // 提示消息
    static notice(options) {
        ElNotification({
            title: '提示消息',
            type: 'success',
            duration: 2000,
            position: 'bottom-right',
            ...options,
        })
    }

    // 确认提示
    static confirm(content, title, config) {
        return ElMessageBox.confirm(
            content,
            title || "警告",
            {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                ...config
            }
        )
    }

    static exceptionHandler(reason) {
        const {code2, message} = reason
        if (code2) {
            ElementUtils.notice({
                title: "请求错误:" + code2,
                type: 'warning',
                duration: 3500,
                message,
            })
            // 主题被隐藏，后退
            if (code2 === 26) {
                window.history.back()
            }
            // router.back();
        } else {
            throw reason
        }
    }
}
